package com.one.controller;

import com.one.pojo.Land;
import com.one.pojo.LandRecord;
import com.one.pojo.Store;
import com.one.service.LandRecordService;
import com.one.service.LandService;
import com.one.service.StoreService;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import com.one.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 土地管理
 * @Author semstouch
 * @Date 2017/4/7
 **/
@Controller
@ControllerAdvice
public class LandController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private LandService landService;
    @Resource
    private StoreService storeService;
    @Resource
    private LandRecordService landRecordService;
    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public LandController(){
        super("callback");
    }

    /**
     * 开地
     * @param request
     * @param token
     * @param userId
     * @return
     */
    @RequestMapping(value = "/openChickenHouse", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult openChickenHouse(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        String landNo = request.getParameter("landNo");
        String houseType= request.getParameter("houseType");
        Land land = new Land();
        land.setChickenfarmId(userId);
        land.setCode(landNo);
        if(houseType.equals("1")){
            land.setChicksNum(300);
            land.setMinNum(new BigDecimal(300));
            land.setMaxNum(new BigDecimal(3000));
            land.setChicksNum(300);
        }else {
            land.setChicksNum(3000);
            land.setMinNum(new BigDecimal(3000));
            land.setMaxNum(new BigDecimal(30000));
            land.setChicksNum(3000);
        }
        Store store = storeService.findByUserId(userId);
        Integer landChicks= land.getChicksNum();
        BigDecimal storeChicks= store.getChicksNum();
        if(storeChicks.intValue()<landChicks){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户小鸡数量不够|Your chicken is not enough");
            return  jsonResult;
        }
        storeChicks=storeChicks.subtract(new BigDecimal(landChicks));
        store.setChicksNum(storeChicks);
        storeService.update(store);
        landService.add(land);
        jsonResult.setCode("1");
        return  jsonResult;
    }

    /**
     * 收获鸡蛋
     * @param request
     * @param token
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getEggsFromLand", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getEggsFromLand(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }

        String landId = request.getParameter("landId");
        Land land=landService.getEggsFromLand(userId, landId);
        if(land==null){
            jsonResult.setCode("-1");
            jsonResult.setMsg("收获失败");
            return jsonResult;
        }
        jsonResult.setCode("1");
        Map rs = new HashMap();
        rs.put("land",land);
        rs.put("storeStatus","0");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    /**
     * 收获小鸡
     * @param request
     * @return
     */
    @RequestMapping(value = "/putChickenToStore", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult putChickenToStore(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        String landId = request.getParameter("landId");
        Land land=landService.putChickenToStore(userId, landId);
        jsonResult.setCode("1");
        Map rs = new HashMap();
        rs.put("land",land);
        rs.put("storeStatus","0");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    /**
     * 孵化鸡蛋
     * @param request
     * @param eggNum
     * @return
     */
    @RequestMapping(value = "/incubateEgg", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult incubateEgg(HttpServletRequest request,String token,String  userId,Integer eggNum) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }

        String landId = request.getParameter("landId");
        Land land=landService.findById(userId,landId);
        Store store = storeService.findByUserId(userId);
        if(store.getEggsNum().intValue()<eggNum){
            jsonResult.setCode("-1");
            jsonResult.setMsg("仓库鸡蛋数不够|Your chicken is not enough");
            return  jsonResult;
        }
        Integer chicksNum= land.getChicksNum()+eggNum;
        BigDecimal eggsNum= store.getEggsNum().subtract(new BigDecimal(eggNum));
        land.setChicksNum(chicksNum);
        store.setEggsNum(eggsNum);
        LandRecord landRecord = new LandRecord();
        landRecord.setUserId(userId);
        landRecord.setChicksNum(new BigDecimal(eggNum));
        landRecord.setType("2");
        landRecord.setChickenlandId(landId);
        landRecord.setLandNo(land.getCode());
        landRecord.setCreateTime(StringUtils.getNowTime());
        landService.update(land,store);
        landRecordService.add(landRecord);


        jsonResult.setCode("1");
        Map rs = new HashMap();
        rs.put("land",land);
        rs.put("storeStatus","0");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    @RequestMapping(value = "/putChickenToLand", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult putChickenToLand(HttpServletRequest request,String token,String  userId,Integer chicksNum) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        String landId = request.getParameter("landId");
        Land land=landService.findById(userId,landId);
        Store store = storeService.findByUserId(userId);
        if(store.getChicksNum().intValue()<chicksNum){
            jsonResult.setCode("-1");
            jsonResult.setMsg("仓库小鸡数不够|Your chicken is not enough");
            return  jsonResult;
        }
        Integer landChicks= land.getChicksNum()+chicksNum;
        BigDecimal storeChicks= store.getChicksNum().subtract(new BigDecimal(chicksNum));
        land.setChicksNum(landChicks);
        store.setChicksNum(storeChicks);
        LandRecord landRecord = new LandRecord();
        landRecord.setUserId(userId);
        landRecord.setChicksNum(new BigDecimal(chicksNum));
        landRecord.setType("1");
        landRecord.setChickenlandId(landId);
        landRecord.setLandNo(land.getCode());
        landRecord.setCreateTime(StringUtils.getNowTime());
        landService.update(land,store);
        landRecordService.add(landRecord);

        jsonResult.setCode("1");
        Map rs = new HashMap();
        rs.put("land",land);
        rs.put("storeStatus","0");
        jsonResult.setData(rs);
        return  jsonResult;
    }

}
